package org.invigorare.auctions;

import org.invigorare.Utils;
import org.invigorare.auctions.data.AppSettings;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AuctionMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Choose SyncMenu or AuctionMenu depending on whether the settings have been initialized already
        // (go to SyncMenu on first launch)
        
        
        setContentView(R.layout.main);
        
        Button bSaleForm = (Button)findViewById(R.id.button_auctionsale_form);
        bSaleForm.setOnClickListener(new ClickListener_LaunchActivity(AuctionSaleForm.class));
        
        Button bSaleList = (Button)findViewById(R.id.button_auctionsale_list);
        bSaleList.setOnClickListener(new ClickListener_LaunchActivity(AuctionSaleList.class, false));
        
        Button bSync = (Button)findViewById(R.id.button_syncmenu);
        bSync.setOnClickListener(new ClickListener_LaunchActivity(SyncMenu.class, false));
    }

    private class ClickListener_LaunchActivity implements View.OnClickListener {
    	
    	Class<? extends Activity> activityClass;
    	boolean forceSettingsConfigured;
    	
    	/**
    	 * By default, forces the user to configure settings before this activity is allowed to run.
    	 * @param activityToLaunch
    	 */
    	public ClickListener_LaunchActivity(Class<? extends Activity> activityToLaunch) {
    		this(activityToLaunch, true);
    	}
    	
    	/**
    	 * 
    	 * @param activityToLaunch
    	 * @param forceSettingsConfigured whether to force settings to be configured before this activity can run
    	 */
    	public ClickListener_LaunchActivity(Class<? extends Activity> activityToLaunch, boolean forceSettingsConfigured) {
    		this.activityClass = activityToLaunch;
    		this.forceSettingsConfigured = forceSettingsConfigured;
    	}
    	
    	public void onClick(View v) {
    		Context context = getApplicationContext();
    		Intent myIntent;
    		if (forceSettingsConfigured && new AppSettings(context).settingsVerified()) {
    			Utils.showToast(AuctionMenu.this, context.getString(R.string.must_config_settings));
    	    	myIntent = new Intent(AuctionMenu.this, SyncMenu.class);
    		} else {
    	    	myIntent = new Intent(AuctionMenu.this, activityClass);
    		}
    		AuctionMenu.this.startActivity(myIntent);
	    }
    }
    

    
}